# Contributor: Alex Yam <alex@alexyam.com>
# Maintainer: Patrycja Rosa <alpine@ptrcnull.me>
pkgname=wasi-libcxx
# match llvm version
pkgver=19.1.2
_llvmver="${pkgver%%.*}"
_wasi_sdk_ver=wasi-sdk-24
pkgrel=1
pkgdesc="WASI LLVM C++ standard library"
url="https://libcxx.llvm.org/"
arch="all"
license="Apache-2.0 WITH LLVM-exception"
makedepends="
	clang
	cmake
	libxml2-dev
	llvm$_llvmver-dev
	llvm$_llvmver-static
	python3-dev
	samurai
	wasi-libc
	zlib-dev
	"
source="
	https://github.com/llvm/llvm-project/releases/download/llvmorg-$pkgver/llvm-project-$pkgver.src.tar.xz
	wasi-sdk-$_wasi_sdk_ver.tar.gz::https://github.com/WebAssembly/wasi-sdk/archive/refs/tags/$_wasi_sdk_ver.tar.gz
	"
builddir="$srcdir"/llvm-project-$pkgver.src
# TODO: check, needs to somehow pass wasi sysroot include to lit..
options="!check"

prepare() {
	default_prepare

	mv "$srcdir"/wasi-sdk-$_wasi_sdk_ver/wasi-sdk.cmake "$builddir"
	mv "$srcdir"/wasi-sdk-$_wasi_sdk_ver/cmake/Platform cmake
}

_configure() {
	local target="$1"
	local destdir="$2"
	local want_threads="OFF"
	local extra_cflags=""

	case "$target" in
	*-threads)
		want_threads="ON"
		extra_cflags="-pthread"
		;;
	esac

	cmake -B "$destdir" -G Ninja -S runtimes -Wno-dev \
		-DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" \
		-DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DCMAKE_MODULE_PATH="$builddir"/cmake \
		-DCMAKE_TOOLCHAIN_FILE="$builddir"/wasi-sdk.cmake \
		-DCMAKE_C_COMPILER_WORKS=ON \
		-DCMAKE_CXX_COMPILER_WORKS=ON \
		-DCMAKE_C_FLAGS="$CFLAGS $extra_cflags --target=$target" \
		-DCMAKE_CXX_FLAGS="$CXXFLAGS $extra_cflags --target=$target" \
		-DCMAKE_ASM_COMPILER_TARGET="$target" \
		-DCMAKE_CXX_COMPILER_TARGET="$target" \
		-DCMAKE_C_COMPILER_TARGET="$target" \
		-DLLVM_DEFAULT_TARGET_TRIPLE="$target" \
		-DCMAKE_STAGING_PREFIX=/usr/share/wasi-sysroot \
		-DCXX_SUPPORTS_CXX11=ON \
		-DLIBCXX_ABI_VERSION=2 \
		-DLIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY=OFF \
		-DLIBCXX_CXX_ABI=libcxxabi \
		-DLIBCXX_CXX_ABI_INCLUDE_PATHS=libcxxabi/include \
		-DLIBCXX_ENABLE_EXCEPTIONS=OFF \
		-DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=OFF \
		-DLIBCXX_ENABLE_FILESYSTEM=OFF \
		-DLIBCXX_ENABLE_SHARED=OFF \
		-DLIBCXX_ENABLE_THREADS=$want_threads \
		-DLIBCXX_HAS_EXTERNAL_THREAD_API=OFF \
		-DLIBCXX_HAS_MUSL_LIBC=ON \
		-DLIBCXX_HAS_PTHREAD_API=$want_threads \
		-DLIBCXX_HAS_WIN32_THREAD_API=OFF \
		-DLIBCXX_INCLUDE_TESTS="$(want_check && echo ON || echo OFF)" \
		-DLIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY=OFF \
		-DLIBCXXABI_ENABLE_EXCEPTIONS=OFF \
		-DLIBCXXABI_ENABLE_PIC=OFF \
		-DLIBCXXABI_ENABLE_SHARED=OFF \
		-DLIBCXXABI_ENABLE_THREADS=$want_threads \
		-DLIBCXXABI_HAS_EXTERNAL_THREAD_API=OFF \
		-DLIBCXXABI_HAS_PTHREAD_API=$want_threads \
		-DLIBCXXABI_HAS_WIN32_THREAD_API=OFF \
		-DLIBCXXABI_INCLUDE_TESTS="$(want_check && echo ON || echo OFF)" \
		-DLIBCXXABI_LIBCXX_INCLUDES="$builddir"/build-libcxx/include/c++/v1 \
		-DLIBCXXABI_LIBCXX_PATH=libcxx \
		-DLIBCXXABI_SILENT_TERMINATE:BOOL=ON \
		-DLIBCXXABI_USE_LLVM_UNWINDER=OFF \
		-DUNIX=ON \
		-DWASI_SDK_PREFIX=/usr \
		-DLIBCXX_INSTALL_INCLUDE_DIR=include/$target/c++/v1 \
		-DLIBCXX_INSTALL_INCLUDE_TARGET_DIR=include/$target/c++/v1 \
		-DLIBCXXABI_INSTALL_INCLUDE_DIR=include/$target/c++/v1
		# last 3 lines copied from Chimera; the config headers record the overall
		# configuration which will differ if threads are enabled
}

build() {
	export CFLAGS="$CFLAGS -fno-exceptions --sysroot=/usr/share/wasi-sysroot"
	export CXXFLAGS="$CXXFLAGS -fno-exceptions --sysroot=/usr/share/wasi-sysroot"

	_configure wasm32-wasi build
	cmake --build build

	_configure wasm32-wasi-threads build-threads
	cmake --build build-threads
}

package() {
	DESTDIR="$pkgdir" cmake --install build
	DESTDIR="$pkgdir" cmake --install build-threads
	mkdir -p "$pkgdir"/usr/share/wasi-sysroot/include/c++/v1

	cd "$pkgdir"/usr/share/wasi-sysroot/lib
	mv -v wasm32-unknown-wasi wasm32-wasi
	mv -v wasm32-unknown-wasi-threads wasm32-wasi-threads
}

sha512sums="
e98ee405f5c30004b39f0de0470400dd2965adf4cda4b47a1d3792ae362ed43eb45e96a9b1689f31e1064e0b39252379270bd95bf8081cf9b92ecb9b371bf43b  llvm-project-19.1.2.src.tar.xz
e8ee706bad58d63a51e1e856745343b10208620f75a501af7ba57db671f564ad1a67d255af281e126246c3c061aa339335cb6f7611e33e2ffe6474b2322ac39c  wasi-sdk-wasi-sdk-24.tar.gz
"
